/*
 * hashtab.h
 *
 * Copyright (C) 2019 Alex A. <coderain@sdf.org>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef _HASHTAB_H_
#define _HASHTAB_H_

#include <stdint.h>
#include <stdlib.h>
#include <string.h>

#define BUCKETS    1024
#define DUPLICATES 128

typedef struct
{
    void *buckets[BUCKETS][DUPLICATES];
    uint8_t lengths[BUCKETS];
    size_t (*hash)(const void*);
    int (*compare)(const void*, const void*);
} hash_table_t;

void *hash_table_search(hash_table_t *ht, void *entry, int auto_add);
void hash_table_delete(hash_table_t *ht, const void *entry);
void **hash_table_enum(hash_table_t *ht, void **current);

static inline void hash_table_init(hash_table_t *ht, size_t (*hash)(const void*), int (*compare)(const void*, const void*))
{
    memset(ht->buckets, 0, sizeof(ht->buckets));
    memset(ht->lengths, 0, sizeof(ht->lengths));
    ht->hash = hash;
    ht->compare = compare;
}

#endif // _HASHTAB_H_
